<template>
  <RbtBread>
    <RbtBreadItem to="/">首页</RbtBreadItem>
    <RbtBreadItem
      v-if="categoryList.top"
      :to="`/category/${categoryList.top.id}`"
      >{{ categoryList.top.name }}</RbtBreadItem
    >
    <Transition name="fade-right" mode="out-in">
      <RbtBreadItem v-if="categoryList.sub" :key="categoryList.sub.id">
        {{ categoryList.sub.name }}
      </RbtBreadItem>
    </Transition>
  </RbtBread>
</template>

<script setup lang="ts">
import useStore from '@/store'
import { useRoute } from 'vue-router'

// 1. 获取二级分类的id
const route = useRoute()
// 2. 获取store中的类目数据
const { category } = useStore()

// 3. 通过计算属性得到 二级类目的名称和id 一级类目的名称和id
const categoryList = computed(() => {
  const obj: any = {}
  category.$state.list.forEach(top => {
    top.children &&
      top.children.forEach(sub => {
        if (sub.id === route.params.id) {
          // 设置二级类目
          obj.sub = { id: sub.id, name: sub.name }
          // 设置一级类目
          obj.top = { id: top.id, name: top.name }
        }
      })
  })
  return obj
})
</script>

<style scoped lang="scss"></style>
